#ifndef __LEARNER_H__
#define __LEARNER_H__
#include "message.h"
#include "network.h"
class Learner {
private:
    int id;
    std::unordered_map<int, Message> acceptedMsgs;
    std::shared_ptr<Network> net;

public:
    Learner(const int id, const std::shared_ptr<Network> &net,
            const std::initializer_list<int> &list)
        : id(id), net(net) {
        for (auto ele : list) {
            acceptedMsgs[ele] = Message{};
        }
    }
    std::string run() {
        for (;;) {
            std::vector<Message> msgVec;
            int ret = net->recvTcpMsg(msgVec);
            if (ret <= 0) {
                continue;
            }
            for (auto &msg : msgVec) {
                std::cout << "Learner  recv Accept " << msg << "\n";
                handleRecevAccept(msg);
                if (chosen(msg) == nullptr) {
                    std::cout << "chosen null" << std::endl;
                    continue;
                }
                return msg.val;
            }
        }
    }
    void handleRecevAccept(Message &acceptMsg) {
        if (acceptedMsgs.find(acceptMsg.from) == acceptedMsgs.end()) {
            std::cout << "error leaner has no such neighbor " << acceptMsg.from
                      << std::endl;
            return;
        }
        Message &hasAcceptedMsg = acceptedMsgs[acceptMsg.from];
        if (hasAcceptedMsg.seq < acceptMsg.seq) {
            acceptedMsgs[acceptMsg.from] = acceptMsg;
        }
    }
    Message *chosen(Message &msg) {
        std::unordered_map<int, int> acceptCount;
        std::unordered_map<int, Message> acceptMsgMap;
        for (auto &acceptedMsg : acceptedMsgs) {
            int proposalNum = acceptedMsg.second.seq;
            acceptCount[proposalNum]++;
            acceptMsgMap[proposalNum] = acceptedMsg.second;
        }
        for (auto &acceptedMsg : acceptMsgMap) {
            if (acceptCount[acceptedMsg.first] > majority()) {
                msg = acceptedMsg.second;
                return &msg;
            }
        }
        return nullptr;
    }
    int majority() { return acceptedMsgs.size() / 2 + 1; }
};
#endif